package edu.upenn.cis.cis550.hw3.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import edu.upenn.cis.cis550.hw3.client.QueryTables;
import edu.upenn.cis.cis550.hw3.client.QueryTablesAsync;
import edu.upenn.cis.cis550.hw3.shared.Relation;

/**
 * This is the main class for generating Web table results for CIS 550 HW1
 * 
 * @author zives
 * 
 */
public class WebTables implements EntryPoint {
    /**
     * The message displayed to the user when the server cannot be reached or
     * returns an error.
     */
    private static final String SERVER_ERROR = "An error occurred while "
            + "attempting to contact the server. Please check your network "
            + "connection and try again.";

    /**
     * Create a remote service proxy to talk to the server-side service.
     */
    private final QueryTablesAsync queryService = GWT.create(QueryTables.class);

    /**
     * This is the entry point method.
     * @return 
     */
    public void onModuleLoad() {
        drawLoginPage();
    }

    
    private void drawLoginPage() {
        
            VerticalPanel mainPanel = new VerticalPanel();
            HorizontalPanel buttons = new HorizontalPanel();
            final Button loginButton = new Button("Log in");
            final Button signupButton = new Button("Sign Up");
            final TextBox nameField = new TextBox();
            final PasswordTextBox passField = new PasswordTextBox();
            final Label errorLabel = new Label();
            final Grid loginGrid = new Grid(2, 2);

            // We can add style names to widgets
            loginButton.addStyleDependentName("login");
            signupButton.addStyleDependentName("login");

            loginGrid.setText(0, 0, "Username:");
            loginGrid.setWidget(0, 1, nameField);
            loginGrid.setText(1, 0, "Password:");
            loginGrid.setWidget(1, 1, passField);
            loginGrid.addStyleName("loginGrid");
            loginGrid.getColumnFormatter().setStyleName(1, "fieldColumn");

            buttons.add(signupButton);
            buttons.add(loginButton);

            // Create the Signup Box
            final DialogBox signupBox = getSignUpBox();

            mainPanel.setWidth("100%");
            mainPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
            mainPanel.add(loginGrid);
            mainPanel.add(buttons);
            mainPanel.add(errorLabel);

            RootPanel.get("mainPanel").add(mainPanel);

            // Focus the cursor on the name field when the app loads
            nameField.setFocus(true);
            nameField.selectAll();

            signupButton.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    signupBox.show();
                    //loginButton.setEnabled(false);
                    loginButton.setFocus(true);
                }
            });
            
            loginButton.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    login();
                }
            });
    }
    
    private void login() {
        AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
            public void onFailure(Throwable caught) {
                
            }

            public void onSuccess(Boolean loginConfirmed) {

                if(loginConfirmed) {
                    Window.alert("Login Correct!");
                } else {
                    Window.alert("Login Incorrect!");
                }
            }
        };
        
        queryService.validatePassword("12", "mypassword1", callback);        
    }

    private DialogBox getSignUpBox() {
        final DialogBox signupBox = new DialogBox();
        final HorizontalPanel closeButtonPanel = new HorizontalPanel();
        closeButtonPanel.setWidth("100%");
        closeButtonPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
        final Button closeButton = new Button("x");
        closeButton.addStyleName("littleX");
        closeButtonPanel.add(closeButton);
        
        final Button signupButton = new Button("Sign Up!");
        final Grid signupGrid = new Grid(5, 2);
        final VerticalPanel signupPanel = new VerticalPanel();

        final TextBox firstName = new TextBox();
        final TextBox lastName = new TextBox();

        signupGrid.setText(0, 0, "First Name: ");
        signupGrid.setWidget(0, 1, firstName);
        signupGrid.setText(1, 0, "Last Name: ");
        signupGrid.setWidget(1, 1, lastName);

       
        signupBox.setText("Sign Up To Penn Photo!");
        signupBox.setTitle("Sign Up Now!");
        signupBox.setAnimationEnabled(true);
        signupBox.setGlassEnabled(true);
        signupPanel.add(closeButtonPanel);
        signupPanel.add(signupGrid);
        signupPanel.add(signupButton);
        signupBox.add(signupPanel);

        closeButton.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                signupBox.hide();
            }
        });
        
        signupButton.addClickHandler(new ClickHandler() {
           public void onClick(ClickEvent event) {
               signUp();
           }
        });

        return signupBox;
    }

    protected void signUp() {
        AsyncCallback<String> callback = new AsyncCallback<String>() {
            public void onFailure(Throwable caught) {
                
            }

            public void onSuccess(String callback) {
                if(callback.equals("success")) {
                    Window.alert("User inserted!");
                } else {
                    Window.alert(callback);
                }
            }
        };
        
        //queryService.addUser("test2", "test2", "test2@test.com", "08/09/10", "M", callback);
        
    }

}
